<?php
namespace app\admin\controller;

use app\BaseController;
use app\common\facade\admin\PgmManageAdmin;

class PgmManage extends BaseController
{
  /**
   * 节目库列表接口
   */
  public function pgmlibList() 
  {
    $param = $this->request->get([
        'offset' => $this->request->get('page/d')?:0,
        'length' => $this->request->get('size/d')?:20,
        'type',
        'sort'
    ]);

    try {
      $pgmlibList = PgmManageAdmin::pgmlibList($param);
    } catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode()); 
    }
    // var_dump($adminList);
    return successJson($pgmlibList);
  }

  /**
   * 节目库编辑接口
   */
  public function pgmlibEdit()
  {
    $param = $this->request->post([
      'pgm_id',
      'audio_chan',
      'if_sign',
      'leader_actor',
      'need_cover',
      'pgm_attr',      
      'pgm_language',
      'pgm_len',
      'pgm_name',
      'pgm_source',
      'pgm_type',
      'remark',
      'start_date',
      'sum_num',
      'wide_screen'
    ]);

    try {
      // $this->validate($param, 'app\common\validate\Material.sceneeditsave');
      $data = PgmManageAdmin::pgmlibAddEditSave($param);
    } catch (ValidateException $e) {
      return validateJson([], $e->getError());
    }  catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
    return successJson($data);
  }

  /**
   * 节目库新增接口
   */
  public function pgmlibadd()
  {    
    $param = $this->request->post([
      'audio_chan',
      'if_sign',
      'leader_actor',
      'need_cover',
      'pgm_attr',      
      'pgm_language',
      'pgm_len',
      'pgm_name',
      'pgm_source',
      'pgm_type',
      'remark',
      'start_date',
      'sum_num',
      'wide_screen'
    ]);

    if(isset($param['pgm_id'])) {
      unset($param['pgm_id']);
    }

    try {
      // $this->validate($param, 'app\common\validate\Material.scenecopysave');
      $data = PgmManageAdmin::pgmlibAddEditSave($param);
    } catch (ValidateException $e) {
      return validateJson([], $e->getError());
    }  catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
    return successJson($data);
  }

  /**
   * 节目库删除接口
   */
  public function pgmlibdel()
  {
    $ids = $this->request->delete();

    try {
      $data = PgmManageAdmin::pgmlibDelete($ids);
    } catch (\Exception $e)
    {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
      
    return successJson($data);
  }

  /* 节目库播放信息列表 */
  public function pgmplayList()
  {
    $param = $this->request->get([
      'offset' => $this->request->get('page/d')?:0,
      'length' => $this->request->get('size/d')?:20,
      'sort',
      'pgm_id'
    ]);

    try {
      $pgmlibList = PgmManageAdmin::pgmplayList($param);
    } catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode()); 
    }
    // var_dump($adminList);
    return successJson($pgmlibList);
  }

  /* 节目库播放添加 */
  public function pgmplayAdd()
  {
    $param = $this->request->post([
      'all_avail',
      'channel_id',
      'e_play_date',
      's_play_date',
      'first_flag',      
      'pgm_id',
    ]);

    try {
      // $this->validate($param, 'app\common\validate\Material.scenecopysave');
      $data = PgmManageAdmin::pgmplayAdd($param);
    } catch (ValidateException $e) {
      return validateJson([], $e->getError());
    }  catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
    return successJson($data);
  }

  /* 节目库播放编辑 */
  public function pgmplayEdit()
  {
    $param = $this->request->put([
      'all_avail',
      'channel_id',
      'e_play_date',
      's_play_date',
      'first_flag',      
      'pgm_id',
      'play_id'
    ]);

    try {
      // $this->validate($param, 'app\common\validate\Material.sceneeditsave');
      $data = PgmManageAdmin::pgmplayEdit($param);
    } catch (ValidateException $e) {
      return validateJson([], $e->getError());
    }  catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
    return successJson($data);
  }

  /* 节目库播放删除 */
  public function pgmplayDel()
  {
    $rows = $this->request->delete();
    try {
      $data = PgmManageAdmin::pgmplayDelete($rows);
    } catch (\Exception $e)
    {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
      
    return successJson($data);
  }

  /* 节目频道播放计划 */
  public function pgmPlayPlan()
  {
    $param = $this->request->get([
      'play_id',
      'pgm_id'
    ]);

    try {
      $pgmplanList = PgmManageAdmin::pgmPlayPlan($param);
    } catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode()); 
    }
    // var_dump($adminList);
    return successJson($pgmplanList);
  }

  /* 节目频道播放计划新增编辑 */
  public function pgmPlayPlanAdd()
  {
    $param = $this->request->post([
      'dayofweek',
      'play_id',
      'pgm_id',
      'play_num',
      'replay_space',
      's_time',
      'time_space'
    ]);

    try {
      // $this->validate($param, 'app\common\validate\Material.scenecopysave');
      $data = PgmManageAdmin::pgmPlayPlanAdd($param);
    } catch (ValidateException $e) {
      return validateJson([], $e->getError());
    }  catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
    return successJson($data);
  }

  public function pgmPlayDetailList()
  {
    $param = $this->request->get([
      'play_id',
      'pgm_id'
    ]);

    try {
      $pgmdetailList = PgmManageAdmin::pgmPlayDetailList($param);
    } catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode()); 
    }
    return successJson($pgmdetailList);
  }

  public function pgmPlayDetailAdd()
  {
    $param = $this->request->post([
      'pgm_id',
      'play_id',
      'list'
    ]);

    try {
      // $this->validate($param, 'app\common\validate\Material.scenecopysave');
      $data = PgmManageAdmin::pgmPlayDetailAdd($param);
    } catch (ValidateException $e) {
      return validateJson([], $e->getError());
    }  catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
    return successJson($data);
  }

  public function pgmPlayDetailEdit()
  {
    $param = $this->request->put([
      'list'
    ]);

    try {
      // $this->validate($param, 'app\common\validate\Material.scenecopysave');
      $data = PgmManageAdmin::pgmPlayDetailEdit($param);
    } catch (ValidateException $e) {
      return validateJson([], $e->getError());
    }  catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
    return successJson($data);
  }

  // 广告库列表
  public function advlibList() {
    $param = $this->request->get([
        'offset' => $this->request->get('page/d')?:0,
        'length' => $this->request->get('size/d')?:20,
        'type',
        'sort',
        'flag' => $this->request->get('flag')?:0,
        'name' => $this->request->get('name')
    ]);

    if(isset($param['flag']) && $param['flag'] == 'all') {
      unset($param['flag']);
    }

    try {
      $advlibList = PgmManageAdmin::advlibList($param);
    } catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode()); 
    }
    // var_dump($adminList);
    return successJson($advlibList);
  }

  /**
   * 广告库编辑接口
   */
  public function advlibEdit()
  {
    $param = $this->request->post([
      'pgm_id',
      'pgm_name',
      'pgm_type',
      'mat_id',
      'markin',
      'markout',      
      'pgm_len',
      's_play_date',
      'e_play_date',
      'creator',
      'create_time',
      'remark',
      'adv_pgm_id',
      'cancel_flag'
    ]);

    try {
      // $this->validate($param, 'app\common\validate\Material.sceneeditsave');
      $data = PgmManageAdmin::advlibAddEditSave($param);
    } catch (ValidateException $e) {
      return validateJson([], $e->getError());
    }  catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
    return successJson($data);
  }

  // 广告库新增接口
  public function advlibAdd()
  {    
    $param = $this->request->post([
      'pgm_id',
      'pgm_name',
      'pgm_type',
      'mat_id',
      'markin',
      'markout',      
      'pgm_len',
      's_play_date',
      'e_play_date',
      'creator',
      'create_time',
      'remark',
      'adv_pgm_id',
      'cancel_flag'
    ]);

    if(isset($param['pgm_id'])) {
      unset($param['pgm_id']);
    }

    try {
      // $this->validate($param, 'app\common\validate\Material.scenecopysave');
      $data = PgmManageAdmin::advlibAddEditSave($param);
    } catch (ValidateException $e) {
      return validateJson([], $e->getError());
    }  catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
    return successJson($data);
  }

  // 广告库删除接口
  public function advlibDel()
  {
    $ids = $this->request->delete();
    try {
      $data = PgmManageAdmin::advlibDel($ids);
    } catch (\Exception $e)
    {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
      
    return successJson($data);
  }

  public function channelgroup()
  {
    try {
      $data = PgmManageAdmin::channelgroup();
    } catch (\Exception $e)
    {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
      
    return successJson($data);
  }

  public function channelgrp()
  {
    $param = $this->request->get([
      'channel'
    ]);

    try {
      $data = PgmManageAdmin::channelgrp($param);
    } catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }

    return successJson($data);
  }

  public function channelgroupAdd()
  {
    $param = $this->request->post([
      'channel_id',
      'pgmgrp_name'
    ]);

    try {
      // $this->validate($param, 'app\common\validate\Material.scenecopysave');
      $data = PgmManageAdmin::channelgroupAddEditSave($param);
    } catch (ValidateException $e) {
      return validateJson([], $e->getError());
    }  catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
    return successJson($data);
  }

  public function channelgroupEdit()
  {
    $param = $this->request->put([
      'channel_id',
      'pgmgrp_id',
      'pgmgrp_name'
    ]);

    try {
      // $this->validate($param, 'app\common\validate\Material.scenecopysave');
      $data = PgmManageAdmin::channelgroupAddEditSave($param);
    } catch (ValidateException $e) {
      return validateJson([], $e->getError());
    }  catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
    return successJson($data);
  }

  public function channelgroupDel()
  {
    $ids = $this->request->delete();
    try {
      $data = PgmManageAdmin::channelgroupDel($ids);
    } catch (\Exception $e)
    {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
      
    return successJson($data);
  }

  public function pgmgroupList() {
    $param = $this->request->get([
      'type',
      'ordername' => $this->request->get('ordername')?:'seq',
      'order' => $this->request->get('order')?:'ascending',
    ]);
    
    try {
      $data = PgmManageAdmin::pgmgroupList($param);
    } catch (\Exception $e)
    {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
      
    return successJson($data);    
  }

  public function pgmgroupAdd() {
    $param = $this->request->post([
      'mat_id',
      'pgmgrp_id',
    ]);
    try {
      $data = PgmManageAdmin::pgmgroupAdd($param);
    } catch (ValidateException $e) {
      return validateJson([], $e->getError());
    } catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
    return successJson($data);
  }

  // 总编室编单获取节目播出明细
  public function pgmLibDetailList() {
    $param = $this->request->get([
      'channel_id',
      'date',
      'sort',
      'page',
      'size'
    ]);

    // var_dump($param);
    try {
      $data = PgmManageAdmin::pgmLibDetailList($param);
    } catch (\Exception $e)
    {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
      
    return successJson($data);
  }

}